package com.ebao.service.talk.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ebao.constants.UserType;
import com.ebao.constants.WebConstants;
import com.ebao.dao.ITDoctorDao;
import com.ebao.dao.ITUserDao;
import com.ebao.dao.base.IBaseJpaDao;
import com.ebao.po.doctor.TDoctor;
import com.ebao.po.talk.TTalk;
import com.ebao.po.user.TUser;
import com.ebao.service.base.impl.CrudServiceImpl;
import com.ebao.service.talk.ITalkPushService;
import com.ebao.service.talk.ITalkService;
import com.ebao.vo.Talk;

@Service("talkService")
public class TalkServiceImpl extends CrudServiceImpl<TTalk> implements ITalkService {

	@Resource(name="talkDao")
	public void setDao(IBaseJpaDao<TTalk> dao) {
		super.setDao(dao);
	}
	
	@Resource
	private ITUserDao userDao;
	@Resource
	private ITDoctorDao doctorDao;
	@Resource
	private ITalkPushService talkPushService;
	
	
	public List<Talk> getUserTalkList(TUser user) {
		List<TTalk> talkList = null;
		// 查询未读记录数
		String jpql1 = "select count(*) from TTalkPush where receiveType=? and receiverId=? and status=0";
		int unreadCount = talkPushService.getCount(jpql1, UserType.COMMON_USER, user.getId());
		if(unreadCount < WebConstants.READ_TALK_SIZE_ONCE) {//不够则添加以前读过的记录，时间倒叙
			String jpql2 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 order by pushDate desc) order by talkDate desc";
			talkList = this.getListByJpql(jpql2, UserType.COMMON_USER, user.getId());
			int size = WebConstants.READ_TALK_SIZE_ONCE - unreadCount;
			String jpql3 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=1 order by pushDate desc) order by talkDate desc";
			talkList.addAll(this.getByJpql(jpql3, 1, size, UserType.COMMON_USER, user.getId()));
			
		} else if (unreadCount > WebConstants.READ_TALK_SIZE_ONCE) {//超过
			String jpql4 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 order by pushDate desc) order by talkDate desc";
			talkList = this.getByJpql(jpql4, 1, WebConstants.READ_TALK_SIZE_ONCE, UserType.COMMON_USER, user.getId());
			
		} else {// 刚刚好
			String jpql5 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 order by pushDate desc) order by talkDate desc";
			talkList = this.getListByJpql(jpql5, UserType.COMMON_USER, user.getId());
		}
		
		// 修改状态为已读
		for(TTalk tk : talkList){
			String jpql6 = "update TTalkPush set status=1 where receiveType=? and receiverId=? and talk.id=?";
			this.executeJpql(jpql6, UserType.COMMON_USER, user.getId(), tk.getId());
		}
		
		List<Talk> talks = talkP2V(talkList); // 方法抽取
		
		return talks;
	}
	
	public int getUserTalkFreshCount(TUser user, String loginTime){
		String jpql = "select count(*) from TTalkPush where receiveType=? and receiverId=? and status=0 and pushDate>?";
		return talkPushService.getCount(jpql, UserType.COMMON_USER, user.getId(), loginTime);
	}
	
	public List<Talk> getUserTalkFresh(TUser user, String loginTime){
		String jpql = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 and pushDate>? order by pushDate desc) order by talkDate desc";
		List<TTalk> talkList = this.getListByJpql(jpql, UserType.COMMON_USER, user.getId(), loginTime);
		
		// 修改状态为已读
		for(TTalk tk : talkList){
			String jpql2 = "update TTalkPush set status=1 where receiveType=? and receiverId=? and talk.id=?";
			this.executeJpql(jpql2, UserType.COMMON_USER, user.getId(), tk.getId());
		}
		
		List<Talk> talks = talkP2V(talkList); // 方法抽取
		
		return talks;
	}

	private List<Talk> talkP2V(List<TTalk> talkList) {
		List<Talk> talks = new ArrayList<Talk>();
		for(TTalk t : talkList) {
			Talk tk = new Talk();
			tk.setId(t.getId());
			tk.setUserType(t.getUserType());
			tk.setReferId(t.getReferId());
			tk.setContent(t.getContent());
			tk.setTalkDate(t.getTalkDate());
			
			if(t.getUserType() == UserType.COMMON_USER){
				TUser u = userDao.getById(t.getReferId());
				tk.setHeadUrl(u.getHeads());//小头像
				tk.setName(u.getUserName());
			} else if (t.getUserType() == UserType.DOCTOR) {
				TDoctor d = doctorDao.getById(t.getReferId());
				tk.setHeadUrl(d.getHeads());//小头像
				tk.setName(d.getUserName());
			}
			
			talks.add(tk);
		}
		return talks;
	}
	
	
	public List<Talk> getDoctorTalkList(TDoctor doctor) {
		List<TTalk> talkList = null;
		// 查询未读记录数
		String jpql1 = "select count(*) from TTalkPush where receiveType=? and receiverId=? and status=0";
		int unreadCount = talkPushService.getCount(jpql1, UserType.DOCTOR, doctor.getId());
		if(unreadCount < WebConstants.READ_TALK_SIZE_ONCE) {//不够则添加以前读过的记录，时间倒叙
			String jpql2 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 order by pushDate desc) order by talkDate desc";
			talkList = this.getListByJpql(jpql2, UserType.DOCTOR, doctor.getId());
			int size = WebConstants.READ_TALK_SIZE_ONCE - unreadCount;
			String jpql3 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=1 order by pushDate desc) order by talkDate desc";
			talkList.addAll(this.getByJpql(jpql3, 1, size, UserType.DOCTOR, doctor.getId()));
			
		} else if (unreadCount > WebConstants.READ_TALK_SIZE_ONCE) {//超过
			String jpql4 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 order by pushDate desc) order by talkDate desc";
			talkList = this.getByJpql(jpql4, 1, WebConstants.READ_TALK_SIZE_ONCE, UserType.DOCTOR, doctor.getId());
			
		} else {// 刚刚好
			String jpql5 = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 order by pushDate desc) order by talkDate desc";
			talkList = this.getListByJpql(jpql5, UserType.DOCTOR, doctor.getId());
		}
		
		// 修改状态为已读
		for(TTalk tk : talkList){
			String jpql6 = "update TTalkPush set status=1 where receiveType=? and receiverId=? and talk.id=?";
			this.executeJpql(jpql6, UserType.DOCTOR, doctor.getId(), tk.getId());
		}
		
		List<Talk> talks = talkP2V(talkList);  // 方法抽取
		
		return talks;
	}
	
	public int getDoctorTalkFreshCount(TDoctor doctor, String loginTime){
		String jpql = "select count(*) from TTalkPush where receiveType=? and receiverId=? and status=0 and pushDate>?";
		return talkPushService.getCount(jpql, UserType.DOCTOR, doctor.getId(), loginTime);
	}
	
	public List<Talk> getDoctorTalkFresh(TDoctor doctor, String loginTime){
		String jpql = "from TTalk t where t.id in (select p.talk.id from TTalkPush p where p.receiveType=? and p.receiverId=? and p.status=0 and pushDate>? order by pushDate desc) order by talkDate desc";
		List<TTalk> talkList = this.getListByJpql(jpql, UserType.DOCTOR, doctor.getId(), loginTime);
		
		// 修改状态为已读
		for(TTalk tk : talkList){
			String jpql2 = "update TTalkPush set status=1 where receiveType=? and receiverId=? and talk.id=?";
			this.executeJpql(jpql2, UserType.DOCTOR, doctor.getId(), tk.getId());
		}
		
		List<Talk> talks = talkP2V(talkList); // 方法抽取
		
		return talks;
	}

}
